package com.egreen.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.asset.dao.BalanceAccountDao;
import com.egreen.asset.dao.OperateBalanceRecordingDao;
import com.egreen.asset.service.IOperateBalanceRecordingService;
import com.egreen.common.pojo.entity.asset.BalanceAccount;
import com.egreen.common.pojo.DTO.asset.BalanceAccountDTO;
import com.egreen.common.pojo.DTO.asset.OperateBalanceRecordingDTO;
import com.egreen.common.pojo.entity.asset.OperateBalanceRecording;
import com.egreen.common.pojo.VO.asset.OperateBalanceRecordingVO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Log
@Service
public class OperateBalanceRecordingServiceImpl extends ServiceImpl<OperateBalanceRecordingDao, OperateBalanceRecording> implements IOperateBalanceRecordingService {
    @Autowired
    BalanceAccountDao balanceAccountDao;

    @Autowired
    OperateBalanceRecordingDao operateBalanceRecordingDao;

    /**
     * 余额增加与减少
     *
     * @param operateBalanceRecordingDTO
     */
    @Override
    public int updateByBalance(OperateBalanceRecordingDTO operateBalanceRecordingDTO) {
        //通过BalanceAccountId获取账户的余额
        BalanceAccount balanceAccount = balanceAccountDao.selectById(operateBalanceRecordingDTO.getBalanceAccountId());
        BigDecimal balance = balanceAccount.getBalance();

        //对用户操作进行判断并将修改后的余额传回去
        if (operateBalanceRecordingDTO.getOperate().equals("增加")) {
            balance = balance.add(operateBalanceRecordingDTO.getOperateBalanceNum());
        } else {
            //前者与后者比较，若前者大则返回1
            //说明减少的金额大于用户余额，所以会取不出 需要抛出异常
            if ((operateBalanceRecordingDTO.getOperateBalanceNum()).compareTo(balanceAccount.getBalance()) == 1) {
                return 0;
            } else {
                balance = balance.subtract(operateBalanceRecordingDTO.getOperateBalanceNum());
            }
        }
        //将修改后的积分余额传回去
        balanceAccount.setBalance(balance);

        //新建操作记录并修改各字段
        OperateBalanceRecording operateBalanceRecording = new OperateBalanceRecording();
        operateBalanceRecording.setBalanceAccountId(operateBalanceRecordingDTO.getBalanceAccountId());
        operateBalanceRecording.setOperate(operateBalanceRecordingDTO.getOperate());
        operateBalanceRecording.setOperatedBalance(balance);
        operateBalanceRecording.setOperateBalanceNum(operateBalanceRecordingDTO.getOperateBalanceNum());

        System.out.println(operateBalanceRecording);
        //对操作记录进行保存
        operateBalanceRecordingDao.insert(operateBalanceRecording);

        //对操作记录进行返回更新
        return balanceAccountDao.updateById(balanceAccount);

    }

    /**
     * 通过用户ID查询余额操作记录
     *
     * @param balanceAccountId
     * @return
     */
    @Override
    public List<OperateBalanceRecordingVO> selectBalanceById(Long balanceAccountId) {
        //存放查询的条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "balance_account_id", "operate", "time", "operate_balance_num", "operated_balance", "deleted");
        //进行筛选
        queryWrapper.eq("balance_account_id", balanceAccountId);
        //根据条件查询得到数据封装到集合里
        List<OperateBalanceRecordingVO> list = operateBalanceRecordingDao.selectList(queryWrapper);
        return list;
    }

    /**
     * 通过ID查找到余额账户信息
     *
     * @param id
     * @return
     */
    @Override
    public BalanceAccountDTO selectBalanceAccountById(Long id) {
        //通过id查询到余额账户
        BalanceAccount balanceAccount = balanceAccountDao.selectById(id);
        BalanceAccountDTO balanceAccountDTO = new BalanceAccountDTO();
        //获取余额账户的id,balance,identity,IdentityType,nickname,avatar后保存到balanceAccountDTO中,并返回
        balanceAccountDTO.setId(balanceAccount.getId());
        balanceAccountDTO.setBalance(balanceAccount.getBalance());
        balanceAccountDTO.setIdentity(balanceAccount.getIdentity());
        balanceAccountDTO.setIdentityType(balanceAccount.getIdentityType());
        balanceAccountDTO.setNickName(balanceAccount.getNickName());
        balanceAccountDTO.setAvatar(balanceAccount.getAvatar());
        return balanceAccountDTO;
    }


}
